package com.learn.cyclicbarrier;

import com.learn.countdown.TestCountDownLatch;
import org.apache.log4j.Logger;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author ln
 * @Description: ${todo}
 * @date 2020/12/30
 */
public class TestCyclicBarrier {
    final static Logger log=Logger.getLogger(TestCyclicBarrier.class);


    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(2);
        CyclicBarrier barrier = new CyclicBarrier(2,()->{
            log.debug("task1,task2 finish.....");
        });

        service.submit(()->{
            try {
                log.debug("task1 begin....");
                TimeUnit.SECONDS.sleep(1);
                barrier.await();//2-1
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        service.submit(()->{
            try {
                log.debug("task2 begin....");
                TimeUnit.SECONDS.sleep(1);
                barrier.await();//1-1=0
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
}
